package com.pig4cloud.pigx.common.customize.enums;

import com.pig4cloud.pigx.common.customize.contants.AbnormalConstants;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 通用 异常 枚举
 */
public class AbnormalEnum {

	@Getter
	@AllArgsConstructor
	public enum MSG {
		/**
		 * 请求常规错误
		 */
		UNKNOWN(10000, AbnormalConstants.UNKNOWN),
		/**
		 * 服务器内部错误
		 */
		DISCONNECTED(10001, AbnormalConstants.DISCONNECTED),
		/**
		 * 请求权限不足
		 */
		UNAUTHORIZED(10002, AbnormalConstants.UNAUTHORIZED),
		/**
		 * 参数不合法
		 */
		UNKNOWN_ORDER_COMPOSITION(10014, AbnormalConstants.UNKNOWN_ORDER_COMPOSITION),
		/**
		 * 该服务不可用
		 */
		SERVICE_SHUTTING_DOWN(10016, AbnormalConstants.SERVICE_SHUTTING_DOWN),
		/**
		 * 不支持此操作
		 */
		UNSUPPORTED_OPERATION(10020, AbnormalConstants.UNSUPPORTED_OPERATION),
		/**
		 * 未发送强制性参数，该参数为空或格式错误
		 */
		MANDATORY_PARAM_EMPTY_OR_MALFORMED(11002, AbnormalConstants.MANDATORY_PARAM_EMPTY_OR_MALFORMED),
		/**
		 * 无效的交易币对
		 */
		BAD_SYMBOL(11021, AbnormalConstants.BAD_SYMBOL),
		/**
		 * 余额不足
		 */
		BALANCE_NOT_SUFFICIENT(20005, AbnormalConstants.BALANCE_NOT_SUFFICIENT),
		/**
		 * 用户找不到
		 */
		OPTIONS_USER_NOT_FOUND(40032, AbnormalConstants.OPTIONS_USER_NOT_FOUND);

		private final int code;
		private final String message;

		/**
		 * 根据name查找对应value
		 *
		 * @param code 枚举名称
		 * @return java.lang.String
		 * @since 1.0.0
		 */
		public static String findMessage(int code) {
			for (AbnormalEnum.MSG item : values()) {
				if (item.getCode() == code) {
					return item.getMessage();
				}
			}

			return null;
		}

		/**
		 * 根据value查找对应的name
		 *
		 * @param message 枚举值
		 * @return java.lang.String
		 * @since 1.0.0
		 */
		public static int findCode(String message) {
			for (AbnormalEnum.MSG item : values()) {
				if (item.getMessage().equals(message)) {
					return item.getCode();
				}
			}
			return -1;
		}
	}
}
